package com.yun.zhong.fu.usercenter.util;

/**
 * @Author RenPu
 * @Date 2022/1/8 11:11
 * @Version 1.0
 * @Description:
 **/
public class ThreadExcuter {


    /**
     * 当前执行线程
     */
    private Thread excuteThead;

    Thread childThread;


    /**
     * 线程的标志位
     */
    private volatile  boolean isRunning=false;


    /**
     * 执行线程的方法
     * @param task
     */
    public void excute(Runnable task){

        excuteThead = new Thread(()->{
            childThread = new Thread(task);
//            childThread.setDaemon(true);
            childThread.start();
            try {
                childThread.join();
                isRunning=true;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        excuteThead.start();
    }


    /**
     * 线程终止方法
     * @param millsn
     */
    public void shoutDown(long millsn){

        long cureentTime = System.currentTimeMillis();
        while (!isRunning){
            if(System.currentTimeMillis()-cureentTime>=millsn){
                excuteThead.interrupt();
//                childThread.interrupt();
            }
        }
        isRunning=false;
    }

    public static void main(String[] args) {

        long startTime = System.currentTimeMillis();
        ThreadExcuter threadExcuter = new ThreadExcuter();
        threadExcuter.excute(()->{
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        threadExcuter.shoutDown(1000);

       long endTime= System.currentTimeMillis();

        System.out.println(endTime-startTime);


    }






}
